/*
 * Copyright (C) 2010 恒康信息科技有限公司
 * 版权所有
 */
package com.jjmmbb.db.db;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;

import com.jjmmbb.anydolist.Util;
import com.jjmmbb.db.pojo.Item;

/**
 * 封装常用的数据库操作<br/>
 * 需要保存到数据库中的对象的属性不能使用基本类型,建议只使用Integer 和 String<br/>
 * 
 * @author yizhe
 * @date 2012-5-18
 */
public class ItemManager {
	Context context;

	private Item pojo;

	public ItemManager(Context context) {
		this.context = context;
	}

	public void setBaseItem(Cursor cursor) {
		pojo = new Item();
		pojo.id = cursor.getInt(cursor.getColumnIndex("id"));
		pojo.title = cursor.getString(cursor.getColumnIndex("title"));
		pojo.team = cursor.getString(cursor.getColumnIndex("team"));
		pojo.time = cursor.getString(cursor.getColumnIndex("time"));
		pojo.timeAlarm = cursor.getLong(cursor.getColumnIndex("timeAlarm"));
		pojo.timeFinish = cursor.getLong(cursor.getColumnIndex("timeFinish"));
		pojo.timePlanComplete = cursor.getLong(cursor
				.getColumnIndex("timePlanComplete"));
		pojo.type = cursor.getInt(cursor.getColumnIndex("type"));
		pojo.typeAlarm = cursor.getInt(cursor.getColumnIndex("typeAlarm"));
	}

	//按时间倒序查询出全部数据
	public ArrayList<Item> getListOrder() {
		String sql = "select * from " + ItemDao.tableName
				+ " order by type,time desc";
		ArrayList<Item> itemList = getListAsSQL(sql);
		return itemList;
	}

	//只显示待办
	public ArrayList<Item> getListOrder0() {
		String sql = "select * from " + ItemDao.tableName
				+ " where type=0 order by type,time desc";
		ArrayList<Item> itemList = getListAsSQL(sql);
		return itemList;
	}

	//隐藏已删除
	public ArrayList<Item> getListOrder01() {
		String sql = "select * from " + ItemDao.tableName
				+ " where type=0 or type=1 order by type,time desc";
		ArrayList<Item> itemList = getListAsSQL(sql);
		return itemList;
	}

	//隐藏已完成
	public ArrayList<Item> getListOrder02() {
		String sql = "select * from " + ItemDao.tableName
				+ " where type=0 or type=2 order by type,time desc";
		ArrayList<Item> itemList = getListAsSQL(sql);
		return itemList;
	}

	public int getCountDoing() {
		String sql = "select id from " + ItemDao.tableName + " where type=0";
		ItemDao dao = new ItemDao(context);
		Cursor cursor = dao.Query(sql);
		int count = cursor.getCount();
		cursor.close();
		dao.close();
		return count;
	}

	/**
	 * 根据sql获取list
	 * 
	 * @param sql
	 * @return
	 */
	public ArrayList<Item> getListAsSQL(String sql) {
		ArrayList<Item> itemList = new ArrayList<Item>();
		ItemDao dao = new ItemDao(context);
		Cursor cursor = dao.Query(sql);
		for (cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()) {
			setBaseItem(cursor);
			itemList.add(pojo);
		}
		cursor.close();
		dao.close();
		return itemList;
	}

	/**
	 * 根据ID获取一条记录
	 * 
	 * @param id
	 * @return
	 */
	public Item getById(int id) {
		pojo = null;
		ItemDao dao = new ItemDao(context);
		Cursor cursor = dao.Query("select * from " + ItemDao.tableName
				+ " where id=" + id);
		if (null != cursor && cursor.getCount() > 0) {
			cursor.moveToFirst();
			setBaseItem(cursor);
		}
		cursor.close();
		dao.close();
		return pojo;
	}

	/**
	 * 返回结果中的提一条记录
	 * 
	 * @param sql
	 * @return
	 */
	public Item getOneAsSQL(String sql) {
		ItemDao dao = new ItemDao(context);
		Cursor cursor = dao.Query(sql);
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			setBaseItem(cursor);
		}
		cursor.close();
		dao.close();
		return pojo;
	}

	/**
	 * 插入一个对象到数据库,支持自动id
	 * 
	 * @param pojo
	 */
	public long insert(Item pojo) {
		if (null == pojo.time || "".equals(pojo.time)) {
			pojo.time = Util.getFormateTime();
		}
		ItemDao dao = new ItemDao(context);

		long result = dao.insert(pojo);
		dao.close();
		return result;
	}

	/**
	 * 插入到数据库,专门用户数据恢复
	 * 
	 * @param pojo
	 */
	public void insert4Restore(String values) {
		String sql = "insert into daiban (title,type,time) values (" + values
				+ ")";
		ItemDao dao = new ItemDao(context);
		dao.execSQL(sql);
		dao.close();
	}

	/**
	 * 插入一个对象到数据库,支持自动id,并且从数据库获取到生成的对象返回(包含ID)
	 * 
	 * @param pojo
	 */
	// public Item insertAndReturn(Item pojo) {
	// if (null == pojo.timeDoing || "".equals(pojo.timeDoing)) {
	// pojo.timeDoing = Util.getFormateTime();
	// }
	// ItemDao dao = new ItemDao(context);
	//
	// long result = dao.insert(pojo);
	// dao.close();
	//
	// Item item = null;
	// if (result != -1) {
	// String sql = "select * from " + ItemDao.tableName
	// + " where title ='" + pojo.title + "'";
	// item = getOneAsSQL(sql); // 获取插入的pojo,用于获取id
	// }
	//
	// return item;
	// }

	/**
	 * 插入一个对象到数据库,支持自动id
	 * 
	 * @param pojo
	 */
	public long insert(String decode) {
		pojo = new Item();
		pojo.title = decode;
		return insert(pojo);
	}

	/**
	 * 删除一个对象,根据id删除
	 * 
	 * @param pojo
	 */
	public int delete(int id) {
		ItemDao dao = new ItemDao(context);
		int result = dao.delete(id);
		dao.close();
		return result;
	}

	/**
	 * 更新一个对象,根据id更新
	 * 
	 */
	public int update(Item pojo) {
		ItemDao dao = new ItemDao(context);
		int result = dao.update(pojo);
		dao.close();
		return result;
	}

	/**
	 * 清空表
	 */
	public void clearTable() {
		ItemDao dao = new ItemDao(context);
		dao.execSQL("delete from " + ItemDao.tableName);
		dao.close();
	}

}
